package com.kk.demo.java8.memory02;

import java.util.stream.IntStream;

/**
 * 进行CPU的乱序的测试
 * @author liujun
 * @date 2016年9月3日
 * @verion 0.0.1
 */
public class PossibleReordering {

    public PossibleReordering() {
    }

    int x, y = 0;

    int a, b = 0;

    public static void doTest(int i) {
        PossibleReordering ordering = new PossibleReordering();

        Thread one = new Thread(() -> {
            ordering.a = 1;
            ordering.x = ordering.b;
        });

        Thread two = new Thread(() -> {
            ordering.b = 1;
            ordering.y = ordering.a;
        });

        one.start();
        two.start();

        try {
            two.join();
            one.join();

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("return Case :" + i + "(" + ordering.x + "," + ordering.y + ")");
    }

    public static void main(String[] args) {
        IntStream.range(0, 100).forEach(PossibleReordering::doTest);
    }
}
